package exsun.jt808.data.util.executorsThread;

import exsun.jt808.data.util.FtpUtils;
import exsun.jt808.data.util.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutorService;

public class FixedThreadHandleRunnable {
    private static final Logger logger = LoggerFactory.getLogger(FixedThreadHandleRunnable.class);

    public static void post(String requrl, String json) {
        Runnable runnable = new Runnable() {
            public void run() {
                String threadName = Thread.currentThread().getName();
                try {
                    String result = HttpUtil.sendPost(requrl, json, 10000, 10000, "UTF-8");
                    logger.info("http,thread:{},{},{},结果:{}", threadName, requrl, json, result);
                }catch (Exception e){
                    logger.error("http,thread:{},{}",threadName , e.getMessage());
                }
            }
        };
        ExecutorService service = FixedThreadHandle.getInst().getService();
        service.execute(runnable);
    }

    public static boolean ftpSave(String host, int port, String username, String password, String basePath,
                           String filePath, String filename, byte[] data, int timeOut) {
        RunnableWithResult runnable = new RunnableWithResult() {
            private boolean runResult = false;
            @Override
            public boolean getRunResult() {
                return runResult;
            }
            public void run() {
                String threadName = Thread.currentThread().getName();
                try {
                    runResult = FtpUtils.uploadFile(host, port, username, password, basePath, filePath, filename, data, timeOut);
                    logger.info("ftp,thread:{},{},{},结果:{}", threadName, filePath, filename, runResult);
                }catch (Exception e){
                    logger.error("ftp,thread:{},{}",threadName , e.getMessage());
                }
            }
        };
        ExecutorService service = FixedThreadHandle.getInst().getService();
        service.execute(runnable);
        return runnable.getRunResult();
    }
}

abstract class RunnableWithResult implements  Runnable{
    public abstract boolean getRunResult();
    public abstract void run();
}
